package zozopo.warehouse;

import java.io.UnsupportedEncodingException;
import java.util.List;
import java.util.Map;

import org.apache.struts2.interceptor.SessionAware;

import zozopo.background.operator.operations.OperatorTB;
import zozopo.background.operator.operations.OperatorlimitedTB;
import zozopo.flow.comm.Flow;
import zozopo.flow.comm.FlowCode;
import zozopo.flow.operations.FlowTB;
import zozopo.flow.operations.RecordlistOS;
import zozopo.flow.operations.RecordlistTB;

import zozopo.warehouse.operations.AftersaleserviceOS;
import zozopo.warehouse.operations.AftersaleserviceTB;


import com.opensymphony.xwork2.Action;
import com.opensymphony.xwork2.ActionSupport;

public class ManageAftersaleServiceAction extends ActionSupport implements Action,SessionAware{
	/**
	 * 
	 */
	private static final long serialVersionUID = 1L;

	private static final String limitedCode ="WAREHOUSE.AFTERSALESERVICE";
	
	//传入参数
	private String searchStr = "";
	private String flowCode = "";
	
	//分页参数
	private int beginPageNum = 0;
	private int pageSize = 20;
	private int rowNum = 0; 
	
	//传出参数
	private List<AftersaleserviceTB> aftersaleserviceList;
	private Map<Integer,RecordlistTB> flowRecordlistMap;
	private Map<String,FlowTB> flowMap;
	
	//各种提示
	int flowReceive ;
	int flowMiss ;
	int flowFaults ;
	int flowCancel;
	int flowConfirmquerstion;
	int flowConfirmship;
	int flowConfirmshipnotpay;
	int flowTrunback;
	int flowAftersaleservice;
	
	private Map<Object, Object> session;
	public void setSession(Map arg0) {
		// TODO Auto-generated method stub
		session = arg0;
	}
	
	public String execute(){

		AftersaleserviceOS aftersaleserviceOS = new AftersaleserviceOS();
		//UserOS userOS = new UserOS();
		Flow flow = new Flow(aftersaleserviceOS.getConn());
		RecordlistOS recordlistOS = new RecordlistOS(aftersaleserviceOS.getConn());
		
		if(!flowCode.equals("")){
			aftersaleserviceList = aftersaleserviceOS.selectByFlowCode(flowCode,FlowCode.WAREHOUSE_AFTERSALESERVICE,beginPageNum, pageSize);
			rowNum = aftersaleserviceOS.getRowNumForFlowCode(flowCode,FlowCode.WAREHOUSE_AFTERSALESERVICE); 
		}else if(searchStr.equals("")){
			aftersaleserviceList = aftersaleserviceOS.selectAll(beginPageNum, pageSize);
			rowNum = aftersaleserviceOS.getRowNum();
		}else{
			aftersaleserviceList = aftersaleserviceOS.selectBySearchStr(searchStr, beginPageNum, pageSize);
			rowNum = aftersaleserviceOS.getRowNumForSearchStr(searchStr);
		}
		
		String inStr = "(";
		boolean isFirst = true;
		
		if(aftersaleserviceList == null){
			return null;
		}
		
		for(AftersaleserviceTB aftersaleservice:aftersaleserviceList){
			if(isFirst){
				inStr += aftersaleservice.getId_aftersaleservice();
				isFirst = false;
			}else{
				inStr += "," + aftersaleservice.getId_aftersaleservice();
			}
		}
		inStr +=")"; 
		
		flowRecordlistMap = recordlistOS.getRecordlistMapByInStr(inStr);
		
		flowMap = flow.getFlowCodeMapByKind(FlowCode.WAREHOUSE_AFTERSALESERVICE);
		
		flowAftersaleservice = flowMap.get("FLOW.WAREHOUSE.AFTERSALESERVICE.START").getNumber_flow();
		 
		flowReceive = flowMap.get("FLOW.WAREHOUSE.AFTERSALESERVICE.RECEIVE").getNumber_flow();
		flowMiss = flowMap.get("FLOW.WAREHOUSE.AFTERSALESERVICE.MISSING").getNumber_flow();
		flowFaults = flowMap.get("FLOW.WAREHOUSE.AFTERSALESERVICE.FAULTS").getNumber_flow();
		flowCancel = flowMap.get("FLOW.WAREHOUSE.AFTERSALESERVICE.CANCEL").getNumber_flow();
		flowConfirmquerstion = flowMap.get("FLOW.WAREHOUSE.AFTERSALESERVICE.CONFIRMQUESTION").getNumber_flow();
		flowConfirmship = flowMap.get("FLOW.WAREHOUSE.AFTERSALESERVICE.CONFIRMSHIP").getNumber_flow();
		flowConfirmshipnotpay = flowMap.get("FLOW.WAREHOUSE.AFTERSALESERVICE.CONFIRMSHIPNOTPAY").getNumber_flow();
		flowTrunback = flowMap.get("FLOW.WAREHOUSE.AFTERSALESERVICE.TRUNBACK").getNumber_flow();

		
		
		aftersaleserviceOS.releaseConnection();
		 
		return SUCCESS;
		
	}
	
	
	public void validate() {
		//权限处理
		OperatorTB operator = (OperatorTB)session.get("operatorInfo"); 
	
		if(operator == null){
			addFieldError("errormessage",getText("operator.timeoutornotlogin"));
			return ; 
		} 
		
		//用户权限
		Map<String,OperatorlimitedTB> operatorlimitedMap = (Map<String,OperatorlimitedTB>)session.get("operatorlimitedMap");
		
		if(operatorlimitedMap.get(limitedCode) == null){
			addFieldError("errormessage","用户没有查看售后信息权限.");
			return;
		}
		
	}
	
	public String getSearchStr() {
		return searchStr;
	}
	public void setSearchStr(String searchStr) throws UnsupportedEncodingException {
		this.searchStr = java.net.URLDecoder.decode(searchStr, "UTF-8");
		this.searchStr = this.searchStr.trim();
	}
	public int getBeginPageNum() {
		return beginPageNum;
	}
	public void setBeginPageNum(int beginPageNum) {
		this.beginPageNum = beginPageNum;
	}
	public int getPageSize() {
		return pageSize;
	}
	public void setPageSize(int pageSize) {
		this.pageSize = pageSize;
	}
	public int getRowNum() {
		return rowNum;
	}
	public void setRowNum(int rowNum) {
		this.rowNum = rowNum;
	}

	public List<AftersaleserviceTB> getAftersaleserviceList() {
		return aftersaleserviceList;
	}

	public void setAftersaleserviceList(
			List<AftersaleserviceTB> aftersaleserviceList) {
		this.aftersaleserviceList = aftersaleserviceList;
	}

	public Map<Integer, RecordlistTB> getFlowRecordlistMap() {
		return flowRecordlistMap;
	}

	public void setFlowRecordlistMap(Map<Integer, RecordlistTB> flowRecordlistMap) {
		this.flowRecordlistMap = flowRecordlistMap;
	}

	public static String getLimitedcode() {
		return limitedCode;
	}

	public int getFlowReceive() {
		return flowReceive;
	}

	public void setFlowReceive(int flowReceive) {
		this.flowReceive = flowReceive;
	}

	public int getFlowMiss() {
		return flowMiss;
	}

	public void setFlowMiss(int flowMiss) {
		this.flowMiss = flowMiss;
	}

	public int getFlowFaults() {
		return flowFaults;
	}

	public void setFlowFaults(int flowFaults) {
		this.flowFaults = flowFaults;
	}

	public int getFlowCancel() {
		return flowCancel;
	}

	public void setFlowCancel(int flowCancel) {
		this.flowCancel = flowCancel;
	}

	public int getFlowConfirmquerstion() {
		return flowConfirmquerstion;
	}

	public void setFlowConfirmquerstion(int flowConfirmquerstion) {
		this.flowConfirmquerstion = flowConfirmquerstion;
	}

	public int getFlowConfirmship() {
		return flowConfirmship;
	}

	public void setFlowConfirmship(int flowConfirmship) {
		this.flowConfirmship = flowConfirmship;
	}

	public int getFlowConfirmshipnotpay() {
		return flowConfirmshipnotpay;
	}

	public void setFlowConfirmshipnotpay(int flowConfirmshipnotpay) {
		this.flowConfirmshipnotpay = flowConfirmshipnotpay;
	}

	public int getFlowTrunback() {
		return flowTrunback;
	}

	public void setFlowTrunback(int flowTrunback) {
		this.flowTrunback = flowTrunback;
	}

	public int getFlowAftersaleservice() {
		return flowAftersaleservice;
	}

	public void setFlowAftersaleservice(int flowAftersaleservice) {
		this.flowAftersaleservice = flowAftersaleservice;
	}

	public Map<String, FlowTB> getFlowMap() {
		return flowMap;
	}

	public void setFlowMap(Map<String, FlowTB> flowMap) {
		this.flowMap = flowMap;
	}

	public String getFlowCode() {
		return flowCode;
	}

	public void setFlowCode(String flowCode) {
		this.flowCode = flowCode;
	}
	
	
}
